﻿namespace BEGDC.CC.Web.Workflow
{
    #region Using Directives

    using System.Collections.Generic;
    using System.Globalization;
    using System.Threading;
    using System.Web;
    using System.Web.UI;
    using ASBCC.Database.Entity;
    using ASBCC.Database.Service;
    using ASBCC.Workflow.K2Manager;

    #endregion

    public class BasePage : Page
    {
        private EmployeeTableEntity _UserInfo;

        public EmployeeTableEntity UserInfo
        {
            get
            {
                return this._UserInfo ?? (this._UserInfo = this.GetEmployeeInfo(HttpContext.Current.User.Identity.Name));
            }
        }

        protected override void InitializeCulture()
        {
            // string lang = "zh-CN";
            string lang = "en-US";

            Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
        }

        protected override void OnInit(System.EventArgs e)
        {
            if (HttpContext.Current == null || string.IsNullOrEmpty(HttpContext.Current.User.Identity.Name))
            {
                this.Response.Redirect("/", true);
                return;
            }

            base.OnInit(e);
        }

        public EmployeeTableEntity GetEmployeeInfo(string cil)
        {
            var employee = new EmployeeTableEntity();
            string condition = string.Format("NameEN='{0}' and IsActived=1", cil);
            employee = EmployeeManager.SelectByCondition(condition)[0];
            return employee;
        }

        public List<int> GetUserInfosByRole(string role_cd)
        {
            EmployeeTableEntitySet set = K2DataHelper.GetUserInfosByROLE_CD(role_cd);
            if (set == null || set.Count == 0)
            {
                return null;
            }
            var userList = new List<int>();
            foreach (EmployeeTableEntity userInfo in set)
            {
                userList.Add(userInfo.EmployeeID.Value);
            }
            return userList;
        }

        #region 消息

        protected virtual void AlertByAjax(string message)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Alert", string.Format("alert('{0}')", message.Replace("'", "''")), true);
        }

        protected virtual void AlertByAjaxAndClose(string message)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Alert", string.Format("alert('{0}');window.close();", message.Replace("'", "''")), true);
        }

        protected virtual void AlertByAjaxAndCloseStartup(string message)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", string.Format("alert('{0}');window.close();", message.Replace("'", "''")), true);
        }

        protected virtual void AlertAndCloseBlock(string message)
        {
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", string.Format("alert('{0}');window.close();", message.Replace("'", "''")), true);
        }

        protected virtual void AlertAndCloseStartup(string message)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "Alert", string.Format("alert('{0}');window.close();", message.Replace("'", "''")), true);
        }

        protected virtual void AlertBlock(string message)
        {
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Alert", string.Format("alert('{0}');", message.Replace("'", "''")), true);
        }

        protected virtual void AlertStartup(string message)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "Alert", string.Format("alert('{0}');", message.Replace("'", "''")), true);
        }

        #endregion
    }
}